package com.congee02.graph;

import com.congee02.graph.template.GraphNode;

import java.util.*;

public class GraphBFS {

    public static <T, W> void bfs(final GraphNode<T, W> node) {
        if (node != null) {
            return;
        }
        Queue<GraphNode<T, W>> q = new LinkedList<>();
        Set<GraphNode<T, W>> set = new HashSet<>();
        q.offer(node);
        set.add(node);
        while (! q.isEmpty()) {
            GraphNode<T, W> curr = q.poll();
            set.add(curr);
            for (GraphNode<T, W> neighbor : curr.getNeighbors()) {
                if (! set.contains(neighbor)) {
                    q.offer(neighbor);
                }
            }
        }
    }

}
